package com.event.controller;


import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * <p>
 * 未读消息 前端控制器
 * </p>
 *
 * @author 孤雏
 * @since 2021-08-09
 */
@RestController
@RequestMapping("/unreadMsg")
@CrossOrigin(origins = "*",maxAge = 3600)
@Api(value = "用户未读消息接口",tags = "用户未读信息接口")
public class UnreadMsgController {
    @Autowired
    RedisTemplate redisTemplate;
    @GetMapping("/selectUnreadMsg")
    @ApiOperation(value = "获取用户未读消息数量接口",notes = "传值进来")
    public JSON selectUnreadMsg(Integer userid){
        Map<Object, Object> map = new ConcurrentHashMap<>();
        Integer unreadMsg = null;
        try {
            unreadMsg = (Integer) redisTemplate.opsForHash().get("UnreadMsg", userid.toString());
            map.put("msg","200");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("msg","500");
        }
        map.put("count",unreadMsg);
        JSON json = (JSON) JSON.toJSON(map);
        return json;
    }
    @PostMapping("/clearUnreadMsg")
    @ApiOperation(value = "未读消息数量清零接口",notes = "传值进来")
    public JSON clearUnreadMsg(Integer userid){
        Map<Object, Object> map = new ConcurrentHashMap<>();
        try {
            redisTemplate.opsForHash().put("UnreadMsg",userid.toString(),0);
            map.put("msg","200");
        } catch (Exception e) {
            map.put("msg","500");
        }
        JSON json = (JSON) JSON.toJSON(map);
        return json;
    }
}
